package org.ajax.servlet;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.ajax.dao.CommdityDAO;
import org.ajax.dao.impl.CommdityDAOImpl;
import org.ajax.entity.Commodity;
import org.ajax.util.DbUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/form")
public class ListFormServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        CommdityDAO dao = new CommdityDAOImpl();
        List<Commodity> commodityList = dao.selectGood();
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String jsonResponse = gson.toJson(commodityList);
        resp.setContentType("application/json");
        PrintWriter out = resp.getWriter();
        out.print(jsonResponse);
        out.flush();
        out.close();
    }
}
